package leetcode.Y2024M5D27;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;

public class S20 {
    public boolean isValid(String s) {
        Deque<Character> stack = new LinkedList<>();
        for (char c : s.toCharArray()) {
            if (stack.isEmpty()) {
                stack.push(c);
            } else {
                Character peek = stack.peek();
                if ((peek == '(' && c == ')') || (peek == '{' && c == '}') || (peek == '[' && c == ']')) {
                    stack.pop();
                } else {
                    stack.push(c);
                }
            }
        }
        return stack.isEmpty();
    }

    @Test
    public void test() {
        String s = "()[]{}";
        System.out.println(isValid(s));
    }

    @Test
    public void test1() {
        String s = "()";
        System.out.println(isValid(s));
    }

    @Test
    public void test2() {
        String s = "((";
        System.out.println(isValid(s));
    }
}
